package model;

import java.sql.ResultSet;
import java.sql.SQLException;

import database.Database;

public class Invitation {
	
	/*
	 * REGION private fields
	 */
	private int ID;
	private Appointment appointment;
	private Person user;
	private int reply;
	private int participating;
	
	/*
	 * REGION public constructors
	 */
	public Invitation(Appointment appointment, Person user){
		setAppointment(appointment);
		setUser(user);
		setReply(false);
		setParticipating(false);
		save();
	}
	
	public Invitation(int ID){
		this.ID = ID;
		this.load();
	}

	/*
	 * REGION public getters
	 */
	public Appointment getAppointment() {
		return appointment;
	}

	public Person getUser() {
		return user;
	}
	
	public boolean getReply() {
		if (reply == 1) {
			return true;
		}
		return false;
	}
	
	public Person getHost(){
		Appointment a = getAppointment();
		return a.getOwner();
	}

	/*
	 * REGION public setters
	 */
	public void setID(int ID) {
		this.ID = ID;
	}
	
	public void setAppointment(Appointment appointment) {
		this.appointment = appointment;
	}

	public void setUser(Person user) {
		this.user = user;
	}

	public void reply(boolean reply){
		setReply(true);
		setParticipating(reply);
		if(reply){
			Database db = new Database();
			db.connectToDatabase();
			db.addAppointment(appointment.getTitle(), appointment.getDate().toString(), appointment.getFromPointOfTime().toString(), appointment.getToPointOfTime().toString(), appointment.getLocation(), user.getID());
		}
	}
	
	public void setReply(boolean r){
		if (r) {
			this.reply = 1;
		}
		else {
			this.reply = 0;
		}
	}

	public void setParticipating(boolean participating) {
		if (participating) {
			this.reply = 1;
		}
		else {
			this.reply = 0;
		}
	}

	public boolean isParticipating() {
		if (this.participating == 1) {
			 return true;
		}
		return false;
	}
	
	public void load(){
		
		// Establish a database connection
		Database database = new Database();
		database.connectToDatabase();
		
		try {
			ResultSet rs = database.getInvitation(ID);
			
			if (rs.next()) {
			
				// Assign variables from the ResultSet to the object
				int meetingID = rs.getInt("meetingID");
				int userID = rs.getInt("userID");
				
				setReply(rs.getBoolean("reply"));
				setAppointment(new Appointment(meetingID));
				setUser(new Person(userID));
				setParticipating(rs.getBoolean("participating"));
			}
			
			else {
				System.out.println("Finner ingen bruker med ID" + ID);
			}
		}
		catch(SQLException e) {
			e.printStackTrace();
		}
		database.disconnectDatabase();
	}
	
	public void load(Appointment appointment, Person user){
		
		// Establish a database connection
		Database database = new Database();
		database.connectToDatabase();
		
		try {
			ResultSet rs = database.getInvitation(appointment, user);
			
			if (rs.next()) {
				
				// Assign variables from the ResultSet to the object
				int meetingID = rs.getInt("meetingID");
				int userID = rs.getInt("userID");
				ID = rs.getInt("id");
				setReply(rs.getBoolean("reply"));
				setAppointment(new Appointment(meetingID));
				setUser(new Person(userID));
				setParticipating(rs.getBoolean("participating"));
			}
			
			else {
				System.out.println("Finner invitasjon med de verdiene");
			}
		}
		catch(SQLException e) {
			e.printStackTrace();
		}
		database.disconnectDatabase();
	}
	
	public void save(){
		// Establish a database connection
		Database database = new Database();
		database.connectToDatabase();
		
		
		try {
			ResultSet rs = database.getInvitation(appointment, user);
			
			if (rs.next()) {
				database.updateInvitation(appointment.getID(), user.getID(), reply, participating);
			}
			
			else {
				database.addInvitation(appointment.getID(), user.getID());
				load(appointment, user);
			}
		}
		catch(SQLException e) {
			e.printStackTrace();
		}
	}
	
	
	
}
